package com.peanut.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabItemBO;
import com.peanut.domain.po.TabItemPO;
import com.peanut.dto.TabItemDTO;
import com.peanut.service.TabItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("item")
@Api("商品管理Api")
public class ItemController {

    @Resource
    private TabItemService itemService;

    /**
     * 添加商品表数据
     * @param dto : 商品表实体类中的数据
     * @return
     */
    @PostMapping
    @ApiOperation("添加商品")
    @ApiImplicitParam(name = "dto",value = "商品表实体类",required = true,paramType ="body")
    public Result addItem(@RequestBody TabItemDTO dto){
        try {
            TabItemBO bo = new TabItemBO();
            BeanUtils.copyProperties(dto,bo);
            int i = itemService.addItem(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 根据ID删除商品
     * @param itemId : 商品ID
     * @return
     */
    @DeleteMapping("/{itemId}")
    @ApiOperation("根据ID删除商品")
    @ApiImplicitParam(name = "itemId",value = "商品ID",required = true,paramType ="path")
    public Result deleteItem(@PathVariable("itemId")Long itemId){
        try {
            int i = itemService.deleteItem(itemId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询商品，并分页
     * @param pageNumber : 当前页数
     * @param pageSize : 每页条数
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation("查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "当前页数",required = true,paramType ="query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType ="query")
    })
    public Result findAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        try {
            Page<TabItemPO> page = itemService.findAll(pageNumber,pageSize);
            return Result.OK().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改商品数据
     * @param dto : 商品表实体类中的数据
     * @return
     */
    @PutMapping
    @ApiOperation("修改商品数据")
    @ApiImplicitParam(name = "dto",value = "商品表实体类",required = true,paramType ="body")
    public Result updateItem(@RequestBody TabItemDTO dto){
        try {
            TabItemBO bo = new TabItemBO();
            BeanUtils.copyProperties(dto,bo);
            int i = itemService.updateItem(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
